home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / OutOfContextMenus / Source / CBlurBehavior.cp next >
Encoding:
Text File  |  1999-06-23  |  4.2 KB  |  157 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    CBlurBehavior.cp                 ©1999 Eric Traut
  3. // ===========================================================================
  4.  
  5. #include "CBlurBehavior.h"
  6. #include "CShadowWindow.h"
  7.  
  8.  
  9. // ---------------------------------------------------------------------------
  10. //        • CBlurBehavior
  11. // ---------------------------------------------------------------------------
  12.  
  13. CBlurBehavior::CBlurBehavior(
  14.     CShadowWindow &        inShadowWindow)
  15.     :    COffscreenBehavior(inShadowWindow, true)
  16. {
  17. }
  18.  
  19.  
  20. // ---------------------------------------------------------------------------
  21. //        • SyncWithShadowWindow
  22. // ---------------------------------------------------------------------------
  23.  
  24. Boolean
  25. CBlurBehavior::SyncWithShadowWindow(void)
  26. {
  27.     Boolean            didSync;
  28.     
  29.     didSync = COffscreenBehavior::SyncWithShadowWindow();
  30.     
  31.     if (didSync)
  32.     {
  33.         // Recalculate our blur rect
  34.         CWindowRecord *        macWindow = mShadowWindow.GetMacWindow();
  35.         mBlurRect = macWindow->port.portRect;
  36.         
  37.         mBlurRect.right -= 15;
  38.         mBlurRect.bottom -= 15;
  39.         mBlurRect.top += 22;
  40.     }
  41.     
  42.     return didSync;
  43. }
  44.  
  45.         
  46. // ---------------------------------------------------------------------------
  47. //        • RenderToGWorld
  48. // ---------------------------------------------------------------------------
  49.  
  50. void
  51. CBlurBehavior::AccumColor(
  52.     UInt16        inPixel, 
  53.     UInt32 &    ioAccumRed, 
  54.     UInt32 &    ioAccumGreen, 
  55.     UInt32 &    ioAccumBlue, 
  56.     Fixed        inFraction)
  57. {
  58.     UInt32        red = (inPixel >> 10) & 0x1F;
  59.     UInt32        green = (inPixel >> 5) & 0x1F;
  60.     UInt32        blue = (inPixel & 0x1F);
  61.     
  62.     ioAccumRed += red * inFraction;
  63.     ioAccumGreen += green * inFraction;
  64.     ioAccumBlue += blue * inFraction;
  65. }
  66.  
  67.  
  68. // ---------------------------------------------------------------------------
  69. //        • RenderToGWorld
  70. // ---------------------------------------------------------------------------
  71.  
  72. Boolean
  73. CBlurBehavior::RenderToGWorld(
  74.     StGWorldLocker &        inBackingLocker,
  75.     StGWorldLocker &        inRenderingLocker)
  76. {
  77.     UInt16        row;
  78.     UInt16        column;
  79.     UInt16        blurCount;
  80.     UInt16        maxRow;
  81.     UInt16        maxColumn;
  82.     UInt16        srcRowWords;
  83.     UInt16        destRowWords;
  84.     UInt16 *    srcRowPtr;
  85.     UInt16 *    destRowPtr;
  86.     UInt16 *    origDestRowPtr;
  87.     PixMapPtr    tempPixMap;
  88.     
  89.     tempPixMap = *inBackingLocker.GetPixMap();
  90.     srcRowWords = (tempPixMap->rowBytes & 0x3FFF) / 2;
  91.     srcRowPtr = reinterpret_cast<UInt16 *>(tempPixMap->baseAddr);
  92.  
  93.     tempPixMap = *inRenderingLocker.GetPixMap();
  94.     destRowWords = (tempPixMap->rowBytes & 0x3FFF) / 2;
  95.     destRowPtr = reinterpret_cast<UInt16 *>(tempPixMap->baseAddr);
  96.     origDestRowPtr = destRowPtr;
  97.  
  98.     maxRow = tempPixMap->bounds.bottom - tempPixMap->bounds.top;
  99.     maxColumn = tempPixMap->bounds.right - tempPixMap->bounds.left;
  100.  
  101.     // Do a blur effect.
  102.     // Sean Parent (Photoshop architect) told me to do it this way.
  103.     for (blurCount = 0; blurCount < 2; blurCount++)
  104.     {
  105.         for (row = 0; row < maxRow; row++)
  106.         {
  107.             for (column = 0; column < maxColumn; column++)
  108.             {
  109.                 UInt32        accumRed = 0;
  110.                 UInt32        accumGreen = 0;
  111.                 UInt32        accumBlue = 0;
  112.                 
  113.                 if (column >= mBlurRect.left && column < mBlurRect.right &&
  114.                     row >= mBlurRect.top && row < mBlurRect.bottom)
  115.                 {
  116.                     if (column > mBlurRect.left)
  117.                         AccumColor(srcRowPtr[column - 1], accumRed, accumGreen, accumBlue, 0x00004000);
  118.                     else
  119.                         AccumColor(srcRowPtr[column], accumRed, accumGreen, accumBlue, 0x00004000);
  120.  
  121.                     if (column < mBlurRect.right - 1)
  122.                         AccumColor(srcRowPtr[column + 1], accumRed, accumGreen, accumBlue, 0x00004000);
  123.                     else
  124.                         AccumColor(srcRowPtr[column], accumRed, accumGreen, accumBlue, 0x00004000);
  125.  
  126.                     if (row > mBlurRect.top)
  127.                         AccumColor(srcRowPtr[column - srcRowWords], accumRed, accumGreen, accumBlue, 0x00004000);
  128.                     else
  129.                         AccumColor(srcRowPtr[column], accumRed, accumGreen, accumBlue, 0x00004000);
  130.  
  131.                     if (row < mBlurRect.bottom - 1)
  132.                         AccumColor(srcRowPtr[column + srcRowWords], accumRed, accumGreen, accumBlue, 0x00004000);
  133.                     else
  134.                         AccumColor(srcRowPtr[column], accumRed, accumGreen, accumBlue, 0x00004000);
  135.                     
  136.                     destRowPtr[column] = ((accumRed >> 16) << 10) |
  137.                                         ((accumGreen >> 16) << 5) |
  138.                                         (accumBlue >> 16);
  139.                 }
  140.                 else
  141.                 {
  142.                     destRowPtr[column] = srcRowPtr[column];
  143.                 }
  144.             }
  145.             
  146.             srcRowPtr += srcRowWords;
  147.             destRowPtr += destRowWords;
  148.         }
  149.         
  150.         srcRowPtr = destRowPtr = origDestRowPtr;
  151.     }
  152.     
  153.     return true;
  154. }
  155.  
  156.  
  157.